package il.ac.braude.sona.control;
import il.ac.braude.sona.constants.Mssg;
import il.ac.braude.sona.constants.P;
import il.ac.braude.sona.constants.Resources;
import il.ac.braude.sona.constants.Statuses;
import il.ac.braude.sona.gui.forms.FormFrame;
import il.ac.braude.sona.gui.forms.FormListener;
import il.ac.braude.sona.gui.forms.myforms.ApproveTimeLineForm;
import il.ac.braude.sona.gui.forms.myforms.EstimateTimeLineForm;
import il.ac.braude.sona.gui.infogui.RequsetInfoFrame;
import il.ac.braude.sona.ocsf.client.Client;
import il.ac.braude.sona.ocsf.client.ServerResponseListener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;
/**
 * The TimeLineController conatins methods to estimate and approve timeline of stages
 * @author Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 */

public class TimeLineController implements FormListener{
	/**
	 *  the form to estimate time to stage
	 */
	private EstimateTimeLineForm estimateTime;
	/**
	 *  the form to approve time to stage
	 */
	private ApproveTimeLineForm approveTime;
	/**
	 *  the time
	 */
	private String time;
	/**
	 *  the request relevant to current stage
	 */
	private RequsetInfoFrame  reqFram;	

	
	@Override
	public void onFormCanceled(FormFrame formFrame) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onFormSubmited(final FormFrame formFrame, ArrayList<Object> values) {
		if (formFrame==estimateTime){	
			if (values.contains("")){
				estimateTime.showMessege("ALL FIELDS MUST BE FILLED!");
			}
			else{
			try {
				Client c = Client.getInstance();
				Mssg mssg;
				values.add(estimateTime.getReqid());
				if (estimateTime.getStage().equals(Statuses.Estimate_Timeline_Surveying))
					 mssg = new Mssg(P.EstimateSurveyorTime,values);
				else 
					 mssg = new Mssg(P.EstimateExecutionTime,values);

				c.sendToServer(new ServerResponseListener() {
					@Override
					public void onServerResponse(Mssg mssg) {
						if (mssg.is(P.error)) {
							estimateTime.showMessege((String) mssg.getMessege());
							return;
						}
						JOptionPane.showMessageDialog(estimateTime,Resources.Strings.EstimateTimeDone);
						formFrame.dispose();
						formFrame.dispose();
						reqFram.dispose();
					}
				},mssg);
			 
			} catch (Exception e) {
				estimateTime.showMessege(Resources.Strings.ConnectFailed);
			}
			}
		}
			
		
		else {
			try {
			Client c = Client.getInstance();
			Mssg mssg;
			values.set(1, approveTime.getReqid());
			if (approveTime.getStage().equals(Statuses.Approve_Surveying_Time))
				 mssg = new Mssg(P.EstimateSurveyorTime,values);
			else 
				 mssg = new Mssg(P.EstimateExecutionTime,values);

			c.sendToServer(new ServerResponseListener() {
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.is(P.error)) {
						approveTime.showMessege((String) mssg.getMessege());
						return;
					}
					try {
						Client c = Client.getInstance();
						Mssg mssg1;
						if (approveTime.getStage().equals(Statuses.Approve_Surveying_Time))
							 mssg1 = new Mssg(P.ApproveSurveyorTime,approveTime.getReqid());
						else 
							 mssg1 = new Mssg(P.ApproveExecutionTime,approveTime.getReqid());

						c.sendToServer(new ServerResponseListener() {
							@Override
							public void onServerResponse(Mssg mssg) {
								if (mssg.is(P.error)) {
									approveTime.showMessege((String) mssg.getMessege());
									return;
								}

								JOptionPane.showMessageDialog(approveTime,Resources.Strings.EstimateTimeApproved);
								formFrame.dispose();
								formFrame.dispose();
								reqFram.dispose();
							}
						},mssg1);
					 
					} catch (Exception e) {
						approveTime.showMessege(Resources.Strings.ConnectFailed);
					}				}
			},mssg);
		 
		} catch (Exception e) {
			approveTime.showMessege(Resources.Strings.ConnectFailed);
		}}

	}
	
	/**
	 * Initiate the form to estimate time line for stage
	 * @param reqid - the relevant request
	 * @param stage - the current stage
	 * @param requsetInfoFrame - the request info fram
	 */
	public void ViewEstimateTimeLineForm(String reqid, String stage, RequsetInfoFrame requsetInfoFrame) {	
		reqFram=requsetInfoFrame;
		estimateTime=new EstimateTimeLineForm(reqid,stage);
		estimateTime.addFormListener(TimeLineController.this);
		estimateTime.showFrame();
		}

	/**
	 * Initiate the form to approve time line for stage, gets the estimated time from DB first
	 * @param reqid - the relevant request
	 * @param stage - the current stage
	 * @param requsetInfoFrame - the request info fram
	 */
	public void viewApproveTimeLineForm(final String reqid, final String stage, RequsetInfoFrame requsetInfoFrame) {
		reqFram=requsetInfoFrame;
		try {
			Client c = Client.getInstance();
			Mssg mssg;
			ArrayList<String> values=new ArrayList<String>();
			values.add(stage);
			values.add(reqid);
				 mssg = new Mssg(P.GetEstimateApprove,values);
			c.sendToServer(new ServerResponseListener() {
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.is(P.error)) {
						JOptionPane.showMessageDialog(estimateTime,"cant get time from DB");
						return;
					}
					else time=new String((String)mssg.getMessege());
					approveTime=new ApproveTimeLineForm(reqid,stage,time,new ActionListener() {
						
						@Override
						public void actionPerformed(ActionEvent e) {
							approveTime.q.setVisible(false);
							approveTime.EstimateTime.setVisible(true);
							approveTime.editDaysButton.disapearButton();
						}});
					approveTime.addFormListener(TimeLineController.this);
					approveTime.showFrame();

				}
			},mssg);
		 
		} catch (Exception e) {
			JOptionPane.showMessageDialog(estimateTime,"cant get time from DB");
		}
		
	}



}
